<template>
  <view class="video_play">
    <image :src="videoObj.img"></image>

    <view class="video_tool">
      <view :class="['iconfont', muted?'iconjingyin':'iconshengyin']" @click="handleMuted"></view>
      <view class="iconfont iconzhuanfa">
        <button open-type="share"></button>
      </view>
    </view>

    <view class="video_wrap">
      <video :src="videoObj.video" :muted="muted"></video>
    </view>

    <view class="download">
      <view class="download_btn" @click="handleDownload">下载高清</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      videoObj: {},
      muted: false
    }
  },
  onLoad() {
    this.videoObj = getApp().globalData.video
  },
  methods: {
    handleMuted() {
      this.muted = !this.muted
    },
    async handleDownload() {
      await uni.showLoading({title: '下载中'})

      const result1 = await uni.downloadFile({
        url: this.videoObj.video
      })
      if(result1[0]) {
        uni.showToast({
          title: '获取资源失败',
          icon: 'none'
        })
        return
      }
      console.log(result1)
      const {tempFilePath} = result1[1]
      const result2 = await uni.saveVideoToPhotosAlbum({
        filePath: tempFilePath
      })
      console.log(result2)
      uni.hideLoading()
      
      if(result2[0] && result2[0].errMsg === 'saveVideoToPhotosAlbum:fail cancel') {
        uni.showToast({
          title: '下载取消',
          icon: 'none'
        })
        return
      }
      await uni.showToast({
        title: '下载成功'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.video_play {
  position: relative;
  image {
    position: absolute;
    width: 100vw;
    height: 100vh;
    z-index: -1;
    filter: blur(20px);
  }

  .video_tool {
    margin-top: 15rpx;
    height: 80rpx;
    display: flex;
    justify-content: flex-end;
    .iconfont {
      width: 80rpx;
      color: #fff;
      font-size: 50rpx;
      border-radius: 40rpx;
      background-color: rgba(0,0,0,.2);
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 20rpx;
    }
    .iconzhuanfa {
      position: relative;
      button {
        position: absolute;
        width: 100%;
        height: 100%;
        opacity: 0;
      }
    }
  }

  .video_wrap {
    margin-top: 15rpx;
    display: flex;
    justify-content: center;
    video {
      width: 448.2rpx;
      height: 796.8rpx;
    }
  }

  .download {
    margin-top: 30rpx;
    display: flex;
    justify-content: center;
    .download_btn {
      width: 360rpx;
      height: 80rpx;
      border-radius: 40rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      border: 4rpx solid #fff;
      background-color: rgba(0,0,0,.5);
    }
  }
}
</style>